package netWork.练习1_聊天室.单线程实现;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;

/**
 * @Package netWork.练习1
 * @Author shimmer
 * @Date 2020/8/2 9:32
 * @TODO
 */
// 缺点：单线程，只能顺序执行（一来一回，你写我读）
public class Client {
    public static void main(String[] args) {
        try {
            Socket socket = new Socket("127.0.0.1", 8888);
            System.out.println("客户端连接成功...");
            DataInputStream is = new DataInputStream(socket.getInputStream());
            DataOutputStream os = new DataOutputStream(socket.getOutputStream());
            Scanner scanner = new Scanner(System.in);
            while (true) {
                os.writeUTF(scanner.nextLine());
                os.flush();
                System.out.println(is.readUTF());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
